package com.skzh.web.controller.drawtask;

import com.skzh.common.core.controller.BaseController;
import com.skzh.common.core.domain.AjaxResult;
import com.skzh.common.core.page.TableDataInfo;
import com.skzh.drawtask.domain.TaskSurface;
import com.skzh.drawtask.domain.TaskSurfacePoints;
import com.skzh.drawtask.service.ISurfaceService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author zr
 * @Description 面任务控制器
 * @Date 2025/3/31 16:11
 */
@RestController
@RequestMapping("/surfaceTask/surfaceInfo")
public class surface extends BaseController {

    @Autowired
    private ISurfaceService surfaceService;

    /**
     * 新增面任务
     */
    @ApiOperation(value = "新增面任务")
    @PostMapping
    public AjaxResult add(@RequestBody TaskSurface taskSurface) {
        return toAjax(surfaceService.insertTaskSurface(taskSurface));
    }

    /**
     * 根据ID删除面任务
     */
    @ApiOperation(value = "根据ID删除面任务")
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable Long id) {
        return toAjax(surfaceService.deleteTaskSurfaceById(id));
    }


    /**
     * 查询面任务
     */
    @ApiOperation(value = "查询面任务")
    @GetMapping("/list")
    public TableDataInfo list(TaskSurface taskSurface) {
            //查询面主体
            List<TaskSurface> taskSurfaces = surfaceService.selectTaskSurfaceList(taskSurface);
            //取出taskSurfaces所有元素的id
            List<Long> surfaceIds = taskSurfaces.stream().map(TaskSurface::getId).collect(Collectors.toList());
            //根据ID批量查询对应面的顶点信息
            List<TaskSurfacePoints> surfacePointsList = surfaceService.selectTaskSurfacePointsByIds(surfaceIds);

            //创建Map来存储每个面ID对应的顶点信息
            Map<Long, List<TaskSurfacePoints>> pointsMap = surfacePointsList.stream()
                    .collect(Collectors.groupingBy(TaskSurfacePoints::getSurfaceId));

            //创建列表存储组合后的面及其顶点信息
            List<TaskSurface> surfaceWithPointsList = new ArrayList<>();
            for (TaskSurface surface : taskSurfaces) {
                TaskSurface surfaceAndPoint = new TaskSurface();
                List<TaskSurfacePoints> taskSurfacePoints = pointsMap.getOrDefault(surface.getId(), Collections.emptyList());
                surfaceAndPoint.setId(surface.getId());
                surfaceAndPoint.setSurfaceName(surface.getSurfaceName());
                surfaceAndPoint.setSurfaceColor(surface.getSurfaceColor());
                surfaceAndPoint.setSurfaceArea(surface.getSurfaceArea());
                surfaceAndPoint.setPoints(taskSurfacePoints.stream()
                        .map(point -> {
                            TaskSurfacePoints surfacePoints = new TaskSurfacePoints();
                            surfacePoints.setMLongitude(point.getMLongitude());
                            surfacePoints.setMLatitude(point.getMLatitude());
                            surfacePoints.setMOrder(point.getMOrder());
                            return surfacePoints;
                        })
                        .collect(Collectors.toList()));
                surfaceWithPointsList.add(surfaceAndPoint);
            }

            return getDataTable(surfaceWithPointsList);
    }

}
